import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/auth/AuthLoginView.vue' // 导入 LoginView 组件

import TaskDashboardPage from '../views/task/TaskDashboardPage.vue'
import TaskPendingTaskPage from '../views/task/TaskPendingTaskPage.vue'
import TaskCompletedTaskPage from '../views/task/TaskCompletedTaskPage.vue'
import TaskMyTaskPage from '../views/task/TaskMyTaskPage.vue'
import TaskMyPlanPage from '../views/task/TaskMyPlanPage.vue'

import IamUserPage from '../views/iam/IamUserPage.vue'
import IamRolePage from '../views/iam/IamRolePage.vue'
import IamRoleAuthPage from '../views/iam/IamRoleAuthPage.vue'
import IamUserRolePage from '../views/iam/IamUserRolePage.vue'
import IamModulePage from '../views/iam/IamModulePage.vue'
import IamPermissionPage from '../views/iam/IamPermissionPage.vue'

import SysMenuPage from '../views/sys/SysMenuPage.vue'
import SysOperationLogPage from '../views/sys/SysOperationLogPage.vue'
import SysParamsPage from '../views/sys/SysParamsPage.vue'

import BaseWorkShopPage from '../views/base/BaseWorkshopPage.vue'
import BaseWareHousePage from '../views/base/BaseWarehousePage.vue'
import BaseCustomerPage from '../views/base/BaseCustomerPage.vue'
import BaseSupplierPage from '../views/base/BaseSupplierPage.vue'

const routes = [
  {
    path: '/',
    redirect: { name: 'login' },
  },
  {
    path: '/login',
    name: 'login', // 补充 name 便于导航
    component: LoginView, // ✅ 组件名与路由语义一致
  },
  {
    path: '/home',
    name: 'home', // 改为小写
    component: HomeView,
    redirect: '/home/dashboard',
    children: [
      //// 控制台
      {
        path: 'dashboard', // 相对路径（自动继承父路径）
        name: 'Dashboard',
        meta: { index: '2',
                title: '首页'},
        component: TaskDashboardPage, // ✅ 组件名语义化
      },
      {
        path: 'pending',
        name: 'pendingTaskPage',

        meta: {
          index: '3',
          title: '待办理任务'},
        component: TaskPendingTaskPage,
      },
      {
        path: 'completed',
        name: 'completedTaskPage',

        meta: {
            index: '4',
            title: '已办理任务'},
        component: TaskCompletedTaskPage,
      },
      {
        path: 'mytasks',
        name: 'myTasksPage',
        meta: {
          index: '5',
          title: '我的任务'},
        component: TaskMyTaskPage,
      },
      {
        path: 'myplans',
        name: 'myPlansPage',
        meta: {
            index: '6',
            title: '我的计划'},
        component: TaskMyPlanPage,
      },
      {
        path: 'base',
        name: 'base',
        meta: {
            index: '7',
            title: '车间资料'},
        component: BaseWorkShopPage,
       },
      {
        path: 'workshop',
        name: 'workShopPage',
        meta: {
            index: '8',
            title: '车间资料'},
        component: BaseWorkShopPage,
       },
       {
        path: 'warehouse',
        name: 'wareHousePage',
        meta: {
            index: '9',
            title: '仓库资料'},
        component: BaseWareHousePage,
       },
       {
        path: 'supplier',
        name: 'supplierPage',
        meta: {
            index: '10',
            title: '供应商资料'},
        component: BaseSupplierPage,
       },
       {
        path: 'customer',
        name: 'customerPage',
        meta: {
            index: '11',
            title: '客户资料'},
        component: BaseCustomerPage,
       },

       //// 用户与权限
      {
        path: 'iam-user',
        name: 'iamUserPage',
        meta: {
            index: '12',
            title: '用户列表'},
        component: IamUserPage,
      },
      {
        path: 'iam-role',
        name: 'iamRolePage',
        meta: {
            index: '13',
            title: '角色列表'},
        component: IamRolePage,
      },
      {
        path: 'iam-user-role',
        name: 'iamUserRolePage',
        meta: {
            index: '14',
            title: '用户角色'},
        component: IamUserRolePage,
      },
      {
        path: 'iam-module',
        name: 'iamModulePage',
        meta: {
            index: '15',
            title: '模块列表'},
        component: IamModulePage,
      },
      {
        path: 'iam-permission',
        name: 'iamPermissionPage',
        meta: {
            index: '16',
            title: '权限列表'},
        component: IamPermissionPage,
      },
      {
        path: 'iam-role-auth',
        name: 'iamRoleAuthPage',
        meta: {
            index: '17',
            title: '角色授权'},
        component: IamRoleAuthPage,
      },
       //// 系统设置
      {
        path: 'sys-params',
        name: 'sysParamsPage',
        meta: {
            index: '18',
            title: '参数设定'},
        component: SysParamsPage,
      },
      {
        path: 'sys-menu',
        name: 'sysMenuPage',
        meta: {
            index: '19',
            title: '页面菜单'},
        component: SysMenuPage,
      },
      {
        path: 'sys-operation-log',
        name: 'sysOperationLogPage',
        meta: {
            index: '20',
            title: '操作日志'},
        component: SysOperationLogPage,
      },



    ],
  },








]
//创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 添加全局路由错误处理
router.onError((error, to) => {
  console.error('路由切换错误:', error)
  console.error('目标路由:', to)

  // 对于路由不存在的错误，不执行跳转
  // 对于路由不存在的错误，不执行跳转
  if (error.message?.includes('No match found')) {
  // 在TypeScript文件中使用类型断言和条件检查的组合方式
  const ElMessage = (window as any).ElMessage;
  if (ElMessage) {
    ElMessage.error({
      message: '您访问的页面不存在',
      duration: 3000,
      showClose: true
    });
  } else {
    // 备选方案：使用console.log或原生alert
    console.warn('您访问的页面不存在');
  }
  return;
}

  // 对于其他严重错误，如果当前不在默认页面，则跳转到默认页面
  // 但只有在确实需要的情况下才执行跳转
  if (router.currentRoute.value.path !== '/home/Dashboard' &&
      !error.message?.includes('NavigationDuplicated')) {
    router.push('/home/Dashboard')
  }
})

// 添加路由导航守卫
router.beforeEach((to, from, next) => {
  console.log(`从 ${from.path} 导航到 ${to.path}`)
  next()
})

router.afterEach((to) => {
  console.log(`导航到 ${to.path} 完成`)
})

export default router
